package com.bong.panpan.bongcharttools.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.bong.panpan.bongcharttools.R;

import java.util.ArrayList;

/**
 * Created by panpan on 2017/9/25.
 */

public class TempCollectListAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<TempCollectItem> items = new ArrayList<>();
    private View.OnClickListener clickListener;
    private boolean isImportEnable=false;

    public TempCollectListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public TempCollectItem getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_collect_temp, parent, false);
            holder.mac = (TextView) convertView.findViewById(R.id.text_mac);
            holder.state = (TextView) convertView.findViewById(R.id.text_state);
            holder.num = (TextView) convertView.findViewById(R.id.text_num);
            holder.info = (TextView) convertView.findViewById(R.id.text_info);
            holder.begin = (Button) convertView.findViewById(R.id.button_begin);
            holder.importData = (Button) convertView.findViewById(R.id.button_import);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        TempCollectItem item = items.get(position);
        holder.mac.setText("MAC:" + item.mac);
        if (item.state.contains("已连接")) {
            holder.state.setTextColor(Color.GREEN);
            if (item.rssi != 0)
                holder.state.setText(item.state + item.rssi + "dBm");
            else
                holder.state.setText(item.state);
        } else {
            holder.state.setTextColor(Color.RED);
            holder.state.setText(item.state);
        }
        holder.num.setText("采集数量:" + item.num);
        holder.info.setText(item.info);
        holder.begin.setId(position * 10);
        holder.importData.setEnabled(isImportEnable);
        holder.importData.setId(position * 10 + 1);
        if (clickListener != null) {
            holder.begin.setOnClickListener(clickListener);
            holder.importData.setOnClickListener(clickListener);
        }
        if (item.isBegin) {
            holder.begin.setText("停止收集");
        } else {
            holder.begin.setText("开始收集");
        }
        return convertView;
    }

    public void setClickListener(View.OnClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setImportEnable(boolean importEnable) {
        isImportEnable = importEnable;
        notifyDataSetChanged();
    }

    public class ViewHolder {
        TextView mac, state, num, info;
        Button begin, importData;
    }

    private boolean isItemExits(String mac) {
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i).mac.equals(mac))
                return true;
        }
        return false;
    }

    public synchronized void modify(TempCollectItem item) {
        if (!isItemExits(item.mac)) {

        }
    }

    public int getItemPosition(String mac) {
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i).mac.equals(mac))
                return i;
        }
        return -1;
    }

    public TempCollectItem getItem(String mac) {
        int position = getItemPosition(mac);
        return items.get(position);
    }

    public synchronized void add(TempCollectItem item) {
        if (!isItemExits(item.mac))
            items.add(item);
        notifyDataSetChanged();
    }

    public synchronized void clear() {
        items.clear();
        notifyDataSetChanged();
    }

    public static class TempCollectItem {
        public String mac = "", state = "未连接", info = "无数据";
        public int num = 0;
        public boolean isBegin = false;
        public int rssi = 0;
    }
}
